﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Web.Script.Serialization;
using System.Reflection;
using System.Linq.Expressions;

namespace VQWEB.common
{
    public class BusinessLayer<TContext> where TContext : DataContext, new() 
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        public T InsertOrUpdate<T>(string request) where T : class
        {
            T item = null;
            DataLayer<TContext> data = new DataLayer<TContext>();
            request = HttpUtility.UrlDecode(request, System.Text.UTF8Encoding.Default);
            T t = serializer.Deserialize<T>(request);
            item = data.InsertOrUpdate<T>(t);
            data.Dispose();
            return item;
        }

        public void Delete<T>(string request) where T : class {
            DataLayer<TContext> layer = new DataLayer<TContext>();
            List<int> l = serializer.Deserialize<List<int>>(request);
            List<object> list = l.Cast<object>().ToList();
            layer.Delete<T>(list);
            layer.Dispose();
        }

        public void LockUnLock<T>(string request, string fielName) where T : class
        {
            DataLayer<TContext> layer = new DataLayer<TContext>();
            List<int> l = serializer.Deserialize<List<int>>(request);
            List<object> list = l.Cast<object>().ToList();
            layer.LoclkUnLock<T>(list, fielName);
            layer.Dispose();
        }

        public void InsertAllInOne<T>(int[] valueFirst, string propertyNameFirst, string propertyNameSecond, object valueSecond) where T : class,new()
        {
            List<object> list = valueFirst.Cast<object>().ToList();
            DataLayer<TContext> data = new DataLayer<TContext>();
            data.InsertAllInOne<T>(list, propertyNameFirst, propertyNameSecond, valueSecond);
            data.Dispose();

        }

        public IList<object> List<T>(int? page, int? pageSize, Expression<Func<T, bool>> predicate, Expression<Func<T, object>> sort, Expression<Func<T, object>> selector) where T : class
        {
            DataLayer<TContext> data = new DataLayer<TContext>();
            return data.List<T>(page, pageSize, predicate, sort, selector);
        
        }

        public IList<T> List<T>(int? page, int? pageSize, Expression<Func<T, bool>> predicate, Expression<Func<T, object>> sort) where T : class
        {
            DataLayer<TContext> data = new DataLayer<TContext>();
            return data.List<T>(page, pageSize, predicate, sort);

        }

        public object Get<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> selector) where T : class
        {
            DataLayer<TContext> data = new DataLayer<TContext>();
            return data.Get<T>(predicate, selector);
        }

        public T Get<T>(Expression<Func<T, bool>> predicate) where T : class
        {
            DataLayer<TContext> data = new DataLayer<TContext>();
            return data.Get<T>(predicate);
        }

        public List<Dictionary<string, object>> RenderArrayCheckboxJSON(IList<object> list_object, List<object> array_id, string fielId, string fielName, string name_array)
        {
            List<Dictionary<string, object>> list_element = new List<Dictionary<string, object>>();
            foreach (var item in list_object)
            {
                Dictionary<string, object> element = new Dictionary<string, object>();
                object vId = null, vName = name_array, vText = null, vChecked = false;
                PropertyInfo[] PropertyInfo = item.GetType().GetProperties();
                foreach (var p in PropertyInfo)
                {

                    if (p.Name.Equals(fielId))
                        vId = p.GetValue(item, null);
                    if (p.Name.Equals(fielName))
                        vText = p.GetValue(item, null);
                    if (array_id != null && array_id.Contains(vId))
                        vChecked = true;

                }
                element.Add("name", vName);
                element.Add("value", vId);
                element.Add("text", vText);
                element.Add("checked", vChecked);
                list_element.Add(element);
            }
            return list_element;
        }

        public List<Dictionary<string, object>> RenderArrayMultiSelectJSON(IList<object> list_object, List<object> array_id, string fielId, string fielText)
        {
            List<Dictionary<string, object>> list_element = new List<Dictionary<string, object>>();
            foreach (var item in list_object)
            {
                Dictionary<string, object> element = new Dictionary<string, object>();
                object vId = null, vText = null, vChecked = false;
                PropertyInfo[] PropertyInfo = item.GetType().GetProperties();
                foreach (var p in PropertyInfo)
                {
                    if (p.Name.Equals(fielId))
                        vId = p.GetValue(item, null);
                    if (p.Name.Equals(fielText))
                        vText = p.GetValue(item, null);
                    if (array_id !=null && array_id.Contains(vId))
                        vChecked = true;
                }
                element.Add("value", vId);
                element.Add("text", vText);
                element.Add("selected", vChecked);
                list_element.Add(element);
            }
            return list_element;
        }
    }
}